
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.core.mail &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-core-mail">
            
  <h1>Source code for django.core.mail</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Tools for sending email.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">settings</span>
<span class="c1"># Imported for backwards compatibility and for the sake</span>
<span class="c1"># of a cleaner namespace. These symbols used to be in</span>
<span class="c1"># django/core/mail.py before the introduction of email</span>
<span class="c1"># backends and the subsequent reorganization (See #10355)</span>
<span class="kn">from</span> <span class="nn">django.core.mail.message</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">DEFAULT_ATTACHMENT_MIME_TYPE</span><span class="p">,</span> <span class="n">BadHeaderError</span><span class="p">,</span> <span class="n">EmailMessage</span><span class="p">,</span>
    <span class="n">EmailMultiAlternatives</span><span class="p">,</span> <span class="n">SafeMIMEMultipart</span><span class="p">,</span> <span class="n">SafeMIMEText</span><span class="p">,</span>
    <span class="n">forbid_multi_line_headers</span><span class="p">,</span> <span class="n">make_msgid</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.core.mail.utils</span> <span class="k">import</span> <span class="n">DNS_NAME</span><span class="p">,</span> <span class="n">CachedDnsName</span>
<span class="kn">from</span> <span class="nn">django.utils.module_loading</span> <span class="k">import</span> <span class="n">import_string</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s1">&#39;CachedDnsName&#39;</span><span class="p">,</span> <span class="s1">&#39;DNS_NAME&#39;</span><span class="p">,</span> <span class="s1">&#39;EmailMessage&#39;</span><span class="p">,</span> <span class="s1">&#39;EmailMultiAlternatives&#39;</span><span class="p">,</span>
    <span class="s1">&#39;SafeMIMEText&#39;</span><span class="p">,</span> <span class="s1">&#39;SafeMIMEMultipart&#39;</span><span class="p">,</span> <span class="s1">&#39;DEFAULT_ATTACHMENT_MIME_TYPE&#39;</span><span class="p">,</span>
    <span class="s1">&#39;make_msgid&#39;</span><span class="p">,</span> <span class="s1">&#39;BadHeaderError&#39;</span><span class="p">,</span> <span class="s1">&#39;forbid_multi_line_headers&#39;</span><span class="p">,</span>
    <span class="s1">&#39;get_connection&#39;</span><span class="p">,</span> <span class="s1">&#39;send_mail&#39;</span><span class="p">,</span> <span class="s1">&#39;send_mass_mail&#39;</span><span class="p">,</span> <span class="s1">&#39;mail_admins&#39;</span><span class="p">,</span>
    <span class="s1">&#39;mail_managers&#39;</span><span class="p">,</span>
<span class="p">]</span>


<div class="viewcode-block" id="get_connection"><a class="viewcode-back" href="../../../topics/email.html#django.core.mail.get_connection">[docs]</a><span class="k">def</span> <span class="nf">get_connection</span><span class="p">(</span><span class="n">backend</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">fail_silently</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Load an email backend and return an instance of it.</span>

<span class="sd">    If backend is None (default), use settings.EMAIL_BACKEND.</span>

<span class="sd">    Both fail_silently and other keyword arguments are used in the</span>
<span class="sd">    constructor of the backend.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">klass</span> <span class="o">=</span> <span class="n">import_string</span><span class="p">(</span><span class="n">backend</span> <span class="ow">or</span> <span class="n">settings</span><span class="o">.</span><span class="n">EMAIL_BACKEND</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">klass</span><span class="p">(</span><span class="n">fail_silently</span><span class="o">=</span><span class="n">fail_silently</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">)</span></div>


<div class="viewcode-block" id="send_mail"><a class="viewcode-back" href="../../../topics/email.html#django.core.mail.send_mail">[docs]</a><span class="k">def</span> <span class="nf">send_mail</span><span class="p">(</span><span class="n">subject</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">from_email</span><span class="p">,</span> <span class="n">recipient_list</span><span class="p">,</span>
              <span class="n">fail_silently</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">auth_user</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">auth_password</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
              <span class="n">connection</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">html_message</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Easy wrapper for sending a single message to a recipient list. All members</span>
<span class="sd">    of the recipient list will see the other recipients in the &#39;To&#39; field.</span>

<span class="sd">    If auth_user is None, use the EMAIL_HOST_USER setting.</span>
<span class="sd">    If auth_password is None, use the EMAIL_HOST_PASSWORD setting.</span>

<span class="sd">    Note: The API for this method is frozen. New code wanting to extend the</span>
<span class="sd">    functionality should use the EmailMessage class directly.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">connection</span> <span class="o">=</span> <span class="n">connection</span> <span class="ow">or</span> <span class="n">get_connection</span><span class="p">(</span>
        <span class="n">username</span><span class="o">=</span><span class="n">auth_user</span><span class="p">,</span>
        <span class="n">password</span><span class="o">=</span><span class="n">auth_password</span><span class="p">,</span>
        <span class="n">fail_silently</span><span class="o">=</span><span class="n">fail_silently</span><span class="p">,</span>
    <span class="p">)</span>
    <span class="n">mail</span> <span class="o">=</span> <span class="n">EmailMultiAlternatives</span><span class="p">(</span><span class="n">subject</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">from_email</span><span class="p">,</span> <span class="n">recipient_list</span><span class="p">,</span> <span class="n">connection</span><span class="o">=</span><span class="n">connection</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">html_message</span><span class="p">:</span>
        <span class="n">mail</span><span class="o">.</span><span class="n">attach_alternative</span><span class="p">(</span><span class="n">html_message</span><span class="p">,</span> <span class="s1">&#39;text/html&#39;</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">mail</span><span class="o">.</span><span class="n">send</span><span class="p">()</span></div>


<div class="viewcode-block" id="send_mass_mail"><a class="viewcode-back" href="../../../topics/email.html#django.core.mail.send_mass_mail">[docs]</a><span class="k">def</span> <span class="nf">send_mass_mail</span><span class="p">(</span><span class="n">datatuple</span><span class="p">,</span> <span class="n">fail_silently</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">auth_user</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                   <span class="n">auth_password</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">connection</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Given a datatuple of (subject, message, from_email, recipient_list), send</span>
<span class="sd">    each message to each recipient list. Return the number of emails sent.</span>

<span class="sd">    If from_email is None, use the DEFAULT_FROM_EMAIL setting.</span>
<span class="sd">    If auth_user and auth_password are set, use them to log in.</span>
<span class="sd">    If auth_user is None, use the EMAIL_HOST_USER setting.</span>
<span class="sd">    If auth_password is None, use the EMAIL_HOST_PASSWORD setting.</span>

<span class="sd">    Note: The API for this method is frozen. New code wanting to extend the</span>
<span class="sd">    functionality should use the EmailMessage class directly.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">connection</span> <span class="o">=</span> <span class="n">connection</span> <span class="ow">or</span> <span class="n">get_connection</span><span class="p">(</span>
        <span class="n">username</span><span class="o">=</span><span class="n">auth_user</span><span class="p">,</span>
        <span class="n">password</span><span class="o">=</span><span class="n">auth_password</span><span class="p">,</span>
        <span class="n">fail_silently</span><span class="o">=</span><span class="n">fail_silently</span><span class="p">,</span>
    <span class="p">)</span>
    <span class="n">messages</span> <span class="o">=</span> <span class="p">[</span>
        <span class="n">EmailMessage</span><span class="p">(</span><span class="n">subject</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">sender</span><span class="p">,</span> <span class="n">recipient</span><span class="p">,</span> <span class="n">connection</span><span class="o">=</span><span class="n">connection</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">subject</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">sender</span><span class="p">,</span> <span class="n">recipient</span> <span class="ow">in</span> <span class="n">datatuple</span>
    <span class="p">]</span>
    <span class="k">return</span> <span class="n">connection</span><span class="o">.</span><span class="n">send_messages</span><span class="p">(</span><span class="n">messages</span><span class="p">)</span></div>


<div class="viewcode-block" id="mail_admins"><a class="viewcode-back" href="../../../topics/email.html#django.core.mail.mail_admins">[docs]</a><span class="k">def</span> <span class="nf">mail_admins</span><span class="p">(</span><span class="n">subject</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">fail_silently</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">connection</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                <span class="n">html_message</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Send a message to the admins, as defined by the ADMINS setting.&quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">settings</span><span class="o">.</span><span class="n">ADMINS</span><span class="p">:</span>
        <span class="k">return</span>
    <span class="n">mail</span> <span class="o">=</span> <span class="n">EmailMultiAlternatives</span><span class="p">(</span>
        <span class="s1">&#39;</span><span class="si">%s%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">EMAIL_SUBJECT_PREFIX</span><span class="p">,</span> <span class="n">subject</span><span class="p">),</span> <span class="n">message</span><span class="p">,</span>
        <span class="n">settings</span><span class="o">.</span><span class="n">SERVER_EMAIL</span><span class="p">,</span> <span class="p">[</span><span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">settings</span><span class="o">.</span><span class="n">ADMINS</span><span class="p">],</span>
        <span class="n">connection</span><span class="o">=</span><span class="n">connection</span><span class="p">,</span>
    <span class="p">)</span>
    <span class="k">if</span> <span class="n">html_message</span><span class="p">:</span>
        <span class="n">mail</span><span class="o">.</span><span class="n">attach_alternative</span><span class="p">(</span><span class="n">html_message</span><span class="p">,</span> <span class="s1">&#39;text/html&#39;</span><span class="p">)</span>
    <span class="n">mail</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">fail_silently</span><span class="o">=</span><span class="n">fail_silently</span><span class="p">)</span></div>


<div class="viewcode-block" id="mail_managers"><a class="viewcode-back" href="../../../topics/email.html#django.core.mail.mail_managers">[docs]</a><span class="k">def</span> <span class="nf">mail_managers</span><span class="p">(</span><span class="n">subject</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">fail_silently</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">connection</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                  <span class="n">html_message</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Send a message to the managers, as defined by the MANAGERS setting.&quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">settings</span><span class="o">.</span><span class="n">MANAGERS</span><span class="p">:</span>
        <span class="k">return</span>
    <span class="n">mail</span> <span class="o">=</span> <span class="n">EmailMultiAlternatives</span><span class="p">(</span>
        <span class="s1">&#39;</span><span class="si">%s%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">EMAIL_SUBJECT_PREFIX</span><span class="p">,</span> <span class="n">subject</span><span class="p">),</span> <span class="n">message</span><span class="p">,</span>
        <span class="n">settings</span><span class="o">.</span><span class="n">SERVER_EMAIL</span><span class="p">,</span> <span class="p">[</span><span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">settings</span><span class="o">.</span><span class="n">MANAGERS</span><span class="p">],</span>
        <span class="n">connection</span><span class="o">=</span><span class="n">connection</span><span class="p">,</span>
    <span class="p">)</span>
    <span class="k">if</span> <span class="n">html_message</span><span class="p">:</span>
        <span class="n">mail</span><span class="o">.</span><span class="n">attach_alternative</span><span class="p">(</span><span class="n">html_message</span><span class="p">,</span> <span class="s1">&#39;text/html&#39;</span><span class="p">)</span>
    <span class="n">mail</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">fail_silently</span><span class="o">=</span><span class="n">fail_silently</span><span class="p">)</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>